package coml.jiyun.hp.mvpextract.base;

import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

import coml.jiyun.hp.mvpextract.R;

public abstract class BaseRlvAdapter<T> extends RecyclerView.Adapter<BaseRlvAdapter.BaseViewHolder>{

    private Context mContext;
    private ArrayList<T> mList;

    public BaseRlvAdapter(Context mContext, ArrayList<T> list) {
        this.mContext = mContext;
        this.mList = list;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(getLayout(), parent, false);
        return new BaseViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {

//        holder.mTv.setText(mList.get(position));
        T t = mList.get(position);
        bindData(holder,t);
    }

    protected abstract void bindData(BaseViewHolder holder, T t);

    protected abstract int getLayout();

    //加载更多
    public void addData(List<T> list){
        if (list != null && list.size()>0){
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    //下拉刷新 清除旧的数据
    public void update(ArrayList<T> list){
        if (list != null && list.size()>0){
            mList.clear();
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public static class BaseViewHolder extends RecyclerView.ViewHolder {

        //Android开发的时候一般不用HashMap
        //为什么不用,因为HashMap消耗内存比较多
        //HashMap<Integer, View> mMap = new HashMap<>();

        //和hashMap一样,都是存储键值对的,但是它更轻量级,
       /* ArrayMap;
        SparseArray*/
        SparseArray<View> mMap = new SparseArray<>();

        public BaseViewHolder(View itemView) {
            super(itemView);
            //父类不值道最终子类子条目的布局究竟有哪些id
            //itemView.findViewById(R.id.xx);
        }

        //用来findviewBYid的
        //findViewById 查找到的控件保存到容器中,下次再调用这个方法的时候,先从容器中找,找不到再findViewByid
        public View findView(@IdRes int id) {
            View view = mMap.get(id);
            if (view == null) {
                view = itemView.findViewById(id);
                mMap.put(id,view);
            }

            return view;
        }
        public void setText(int tv, String name) {
             TextView view = (TextView) findView(tv);
             view.setText(name);
        }
    }
    public interface onItemClickListener{
        void onItemClick(View v, int position);
    }
    private onItemClickListener mOnItemClickListener;

    public void setmOnItemClickListener(onItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface onItemLongClickListener{
        void onItemClick(View v, int position);
    }

    private onItemLongClickListener mOnItemLongClickListener;

    public void setmOnItemLongClickListener(onItemLongClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }
}